home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevdljm.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.4 KB  |  313 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.  
  3.    This program may be distributed as part of AFPL Ghostscript, under the
  4.    terms of the Aladdin Free Public License (the "License").
  5.  
  6.    AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No
  7.    author or distributor accepts any responsibility for the consequences of
  8.    using it, or for whether it serves any particular purpose or works at
  9.    all, unless he or she says so in writing.  Refer to the License for full
  10.    details.
  11.  
  12.    Every copy of AFPL Ghostscript must include a copy of the License,
  13.    normally in a plain ASCII text file named PUBLIC.  The License grants you
  14.    the right to copy, modify and redistribute AFPL Ghostscript, but only
  15.    under certain conditions described in the License.  Among other things,
  16.    the License requires that the copyright notice and this notice be
  17.    preserved on all copies.
  18.  
  19.    This program may also be distributed and/or modified under the terms of
  20.    the GNU General Public License as published by the Free Software
  21.    Foundation (the "GPL"); either version 2 of the GPL, or (at your option)
  22.    any later version.
  23.  
  24.    When distributed under the terms of the GPL, this program is distributed
  25.    in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  26.    even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  27.    PURPOSE.  See the GPL for more details.
  28.  
  29.    If this program is being distributed under the terms of the GPL, you
  30.    should have received a copy of the GPL along with this program, normally
  31.    in a plain ASCII text file named COPYING; if not, write to the Free
  32.    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111
  33.    U.S.A.
  34.  */
  35.  
  36. /*$Id: gdevdljm.c,v 1.4 2000/09/19 19:00:12 lpd Exp $ */
  37. /* Generic monochrome H-P DeskJet/LaserJet driver */
  38. #include "gdevprn.h"
  39. #include "gdevdljm.h"
  40.  
  41. /*
  42.  * Thanks for various improvements to:
  43.  *      Jim Mayer (mayer@wrc.xerox.com)
  44.  *      Jan-Mark Wams (jms@cs.vu.nl)
  45.  *      Frans van Hoesel (hoesel@chem.rug.nl)
  46.  *      George Cameron (g.cameron@biomed.abdn.ac.uk)
  47.  *      Nick Duffek (nsd@bbc.com)
  48.  * Thanks for the FS-600 driver to:
  49.  *    Peter Schildmann (peter.schildmann@etechnik.uni-rostock.de)
  50.  * Thanks for the LJIIID duplex capability to:
  51.  *      PDP (Philip) Brown (phil@3soft-uk.com)
  52.  * Thanks for the OCE 9050 driver to:
  53.  *      William Bader (wbader@EECS.Lehigh.Edu)
  54.  * Thanks for the LJ4D duplex capability to:
  55.  *    Les Johnson <les@infolabs.com>
  56.  */
  57.  
  58. /* See gdevdljm.h for the definitions of the PCL_ features. */
  59.  
  60. /* The number of blank lines that make it worthwhile to reposition */
  61. /* the cursor. */
  62. #define MIN_SKIP_LINES 7
  63.  
  64. /* We round up the LINE_SIZE to a multiple of a ulong for faster scanning. */
  65. #define W sizeof(word)
  66.  
  67. /* Send a page to the printer. */
  68. int
  69. dljet_mono_print_page(gx_device_printer * pdev, FILE * prn_stream,
  70.               int dots_per_inch, int features, const char *page_init)
  71. {
  72.     return dljet_mono_print_page_copies(pdev, prn_stream, 1, dots_per_inch,
  73.                     features, page_init);
  74. }
  75. int
  76. dljet_mono_print_page_copies(gx_device_printer * pdev, FILE * prn_stream,
  77.                  int num_copies, int dots_per_inch, int features,
  78.                  const char *page_init)
  79. {
  80.     int line_size = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
  81.     int line_size_words = (line_size + W - 1) / W;
  82.     uint storage_size_words = line_size_words * 8;    /* data, out_row, out_row_alt, prev_row */
  83.     word *storage;
  84.     word
  85.     *data_words,
  86.     *out_row_words,
  87.     *out_row_alt_words,
  88.     *prev_row_words;
  89. #define data ((byte *)data_words)
  90. #define out_row ((byte *)out_row_words)
  91. #define out_row_alt ((byte *)out_row_alt_words)
  92. #define prev_row ((byte *)prev_row_words)
  93.     byte *out_data;
  94.     int x_dpi = pdev->x_pixels_per_inch;
  95.     int y_dpi = pdev->y_pixels_per_inch;
  96.     int y_dots_per_pixel = dots_per_inch / y_dpi;
  97.     int num_rows = dev_print_scan_lines(pdev);
  98.  
  99.     int out_count;
  100.     int compression = -1;
  101.     static const char *const from2to3 = "\033*b3M";
  102.     static const char *const from3to2 = "\033*b2M";
  103.     int penalty_from2to3 = strlen(from2to3);
  104.     int penalty_from3to2 = strlen(from3to2);
  105.     int paper_size = gdev_pcl_paper_size((gx_device *) pdev);
  106.     int code = 0;
  107.     bool dup = pdev->Duplex;
  108.     bool dupset = pdev->Duplex_set >= 0;
  109.  
  110.     if (num_copies != 1 && !(features & PCL_CAN_PRINT_COPIES))
  111.     return gx_default_print_page_copies(pdev, prn_stream, num_copies);
  112.     storage =
  113.     (ulong *)gs_alloc_byte_array(pdev->memory, storage_size_words, W,
  114.                      "hpjet_print_page");
  115.     if (storage == 0)        /* can't allocate working area */
  116.     return_error(gs_error_VMerror);
  117.     data_words = storage;
  118.     out_row_words = data_words + (line_size_words * 2);
  119.     out_row_alt_words = out_row_words + (line_size_words * 2);
  120.     prev_row_words = out_row_alt_words + (line_size_words * 2);
  121.     /* Clear temp storage */
  122.     memset(data, 0, storage_size_words * W);
  123.  
  124.     /* Initialize printer. */
  125.     if (pdev->PageCount == 0) {
  126.     fputs("\033E", prn_stream);    /* reset printer */
  127.     /* If the printer supports it, set the paper size */
  128.     /* based on the actual requested size. */
  129.     if (features & PCL_CAN_SET_PAPER_SIZE) {
  130.         fprintf(prn_stream, "\033&l%dA", paper_size);
  131.     }
  132.     /* If printer can duplex, set duplex mode appropriately. */
  133.     if (features & PCL_HAS_DUPLEX) {
  134.         if (dupset && dup)
  135.         fputs("\033&l1S", prn_stream);
  136.         else if (dupset && !dup)
  137.         fputs("\033&l0S", prn_stream);
  138.         else        /* default to duplex for this printer */
  139.         fputs("\033&l1S", prn_stream);
  140.     }
  141.     }
  142.     /* Put out per-page initialization. */
  143.     fputs("\033&l0o0l0E", prn_stream);
  144.     fputs(page_init, prn_stream);
  145.     fprintf(prn_stream, "\033&l%dX", num_copies);    /* # of copies */
  146.  
  147.     /* End raster graphics, position cursor at top. */
  148.     fputs("\033*rB\033*p0x0Y", prn_stream);
  149.  
  150.     /* The DeskJet and DeskJet Plus reset everything upon */
  151.     /* receiving \033*rB, so we must reinitialize graphics mode. */
  152.     if (features & PCL_END_GRAPHICS_DOES_RESET) {
  153.     fputs(page_init, prn_stream);
  154.     fprintf(prn_stream, "\033&l%dX", num_copies);    /* # of copies */
  155.     }
  156.  
  157.     /* Set resolution. */
  158.     fprintf(prn_stream, "\033*t%dR", x_dpi);
  159.  
  160.     /* Send each scan line in turn */
  161.     {
  162.     int lnum;
  163.     int num_blank_lines = 0;
  164.     word rmask = ~(word) 0 << (-pdev->width & (W * 8 - 1));
  165.  
  166.     /* Transfer raster graphics. */
  167.     for (lnum = 0; lnum < num_rows; lnum++) {
  168.         register word *end_data =
  169.         data_words + line_size_words;
  170.  
  171.         code = gdev_prn_copy_scan_lines(pdev, lnum,
  172.                         (byte *) data, line_size);
  173.         if (code < 0)
  174.         break;
  175.         /* Mask off 1-bits beyond the line width. */
  176.         end_data[-1] &= rmask;
  177.         /* Remove trailing 0s. */
  178.         while (end_data > data_words && end_data[-1] == 0)
  179.         end_data--;
  180.         if (end_data == data_words) {    /* Blank line */
  181.         num_blank_lines++;
  182.         continue;
  183.         }
  184.         /* We've reached a non-blank line. */
  185.         /* Put out a spacing command if necessary. */
  186.         if (num_blank_lines == lnum) {
  187.         /* We're at the top of a page. */
  188.         if (features & PCL_ANY_SPACING) {
  189.             if (num_blank_lines > 0)
  190.             fprintf(prn_stream, "\033*p+%dY",
  191.                 num_blank_lines * y_dots_per_pixel);
  192.             /* Start raster graphics. */
  193.             fputs("\033*r1A", prn_stream);
  194.         } else if (features & PCL_MODE_3_COMPRESSION) {
  195.             /* Start raster graphics. */
  196.             fputs("\033*r1A", prn_stream);
  197. #if 1                /* don't waste paper */
  198.             if (num_blank_lines > 0)
  199.             fputs("\033*b0W", prn_stream);
  200.             num_blank_lines = 0;
  201. #else
  202.             for (; num_blank_lines; num_blank_lines--)
  203.             fputs("\033*b0W", prn_stream);
  204. #endif
  205.         } else {
  206.             /* Start raster graphics. */
  207.             fputs("\033*r1A", prn_stream);
  208.             for (; num_blank_lines; num_blank_lines--)
  209.             fputs("\033*bW", prn_stream);
  210.         }
  211.         }
  212.         /* Skip blank lines if any */
  213.         else if (num_blank_lines != 0) {
  214.         /*
  215.          * Moving down from current position causes head motion
  216.          * on the DeskJet, so if the number of lines is small,
  217.          * we're better off printing blanks.
  218.          */
  219.         /*
  220.          * For Canon LBP4i and some others, <ESC>*b<n>Y doesn't
  221.          * properly clear the seed row if we are in compression mode
  222.          * 3.
  223.          */
  224.         if ((num_blank_lines < MIN_SKIP_LINES && compression != 3) ||
  225.             !(features & PCL_ANY_SPACING)
  226.             ) {
  227.             bool mode_3ns =
  228.             (features & PCL_MODE_3_COMPRESSION) &&
  229.             !(features & PCL_ANY_SPACING);
  230.  
  231.             if (mode_3ns && compression != 2) {
  232.             /* Switch to mode 2 */
  233.             fputs(from3to2, prn_stream);
  234.             compression = 2;
  235.             }
  236.             if (features & PCL_MODE_3_COMPRESSION) {
  237.             /* Must clear the seed row. */
  238.             fputs("\033*b1Y", prn_stream);
  239.             num_blank_lines--;
  240.             }
  241.             if (mode_3ns) {
  242.             for (; num_blank_lines; num_blank_lines--)
  243.                 fputs("\033*b0W", prn_stream);
  244.             } else {
  245.             for (; num_blank_lines; num_blank_lines--)
  246.                 fputs("\033*bW", prn_stream);
  247.             }
  248.         } else if (features & PCL3_SPACING) {
  249.             fprintf(prn_stream, "\033*p+%dY",
  250.                 num_blank_lines * y_dots_per_pixel);
  251.         } else {
  252.             fprintf(prn_stream, "\033*b%dY",
  253.                 num_blank_lines);
  254.         }
  255.         /* Clear the seed row (only matters for */
  256.         /* mode 3 compression). */
  257.         memset(prev_row, 0, line_size);
  258.         }
  259.         num_blank_lines = 0;
  260.  
  261.         /* Choose the best compression mode */
  262.         /* for this particular line. */
  263.         if (features & PCL_MODE_3_COMPRESSION) {
  264.         /* Compression modes 2 and 3 are both */
  265.         /* available.  Try both and see which one */
  266.         /* produces the least output data. */
  267.         int count3 = gdev_pcl_mode3compress(line_size, data,
  268.                             prev_row, out_row);
  269.         int count2 = gdev_pcl_mode2compress(data_words, end_data,
  270.                             out_row_alt);
  271.         int penalty3 =
  272.             (compression == 3 ? 0 : penalty_from2to3);
  273.         int penalty2 =
  274.             (compression == 2 ? 0 : penalty_from3to2);
  275.  
  276.         if (count3 + penalty3 < count2 + penalty2) {
  277.             if (compression != 3)
  278.             fputs(from2to3, prn_stream);
  279.             compression = 3;
  280.             out_data = out_row;
  281.             out_count = count3;
  282.         } else {
  283.             if (compression != 2)
  284.             fputs(from3to2, prn_stream);
  285.             compression = 2;
  286.             out_data = out_row_alt;
  287.             out_count = count2;
  288.         }
  289.         } else if (features & PCL_MODE_2_COMPRESSION) {
  290.         out_data = out_row;
  291.         out_count = gdev_pcl_mode2compress(data_words, end_data,
  292.                            out_row);
  293.         } else {
  294.         out_data = data;
  295.         out_count = (byte *) end_data - data;
  296.         }
  297.  
  298.         /* Transfer the data */
  299.         fprintf(prn_stream, "\033*b%dW", out_count);
  300.         fwrite(out_data, sizeof(byte), out_count,
  301.            prn_stream);
  302.     }
  303.     }
  304.  
  305.     /* end raster graphics and eject page */
  306.     fputs("\033*rB\f", prn_stream);
  307.  
  308.     /* free temporary storage */
  309.     gs_free_object(pdev->memory, storage, "hpjet_print_page");
  310.  
  311.     return code;
  312. }
  313.